﻿Public Class clsAutoNumber
    Public Function getNextKeyNo(code As String, key As String) As Integer
        Dim szSql As String = ""
        szSql = "SELECT no_value"
        szSql &= " FROM sf.t_auto_number WHERE no_key = ? and code=?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, key, code)
        If IsNothing(dt) Then
            getNextKeyNo = 0
        Else
            getNextKeyNo = dt.Rows(0)("no_value") + 1
        End If

    End Function

    Public Sub setNextKeyNo(code As String, key As String)
        Dim szSql As String
        szSql = String.Empty
        If CInt(getNextKeyNo(code, key)) = 0 Then
            szSql &= "INSERT INTO sf.t_auto_number(no_key, no_value, record_date, code) VALUES (?, ?,?,?)"
            DBacs.ExecuteUpdateSql(szSql, -1, key, getNextKeyNo(code, key), Now.ToString("yyyyMMdd"), code)
        Else
            szSql &= "update sf.t_auto_number set no_value = ? , record_date =?, code = ?  where no_key = ?"
            DBacs.ExecuteUpdateSql(szSql, -1, getNextKeyNo(code, key), Now.ToString("yyyyMMdd"), code, key)
        End If
    End Sub
End Class
